//https://gitee.com/api/v5/swagger#/getV5Emails
const https = require('node:https');  
  
let url='https://gitee.com/api/v5/emails?access_token=b777bd6c51c091daad2c7ffdc84e2541';
  




class Gitee{
    constructor(access_token){
        this.access_token=access_token;
    }
    send(url,method='GET',data={}){
        return new Promise((succress,error)=>{
            const options = {
                hostname: 'gitee.com',
                port: 443,
                path: url,
                method:method
            };
            if(method=='POST'||method=='PATCH'){
                options.headers={
                    'Content-Type': 'application/json;charset=UTF-8',
                }
            }

            
            let request=https.request(options, (res) => {
                let data='';    
                res.on('data', (dataItem) => {
                    data += dataItem; 
                });
                res.on('end', () => {
                    if(method=='GET'){
                        data=JSON.parse(data);
                    }
                    succress(data);

                });
            });
            if(method=='POST' || method=='PATCH'){
              request.write(data);
            }
            
            request.end();
            
          
        });
       
    }
  
    async getCodelist(){
        
        let url='/api/v5/gists';
        url+='?access_token='+this.access_token;
        //附加属性
        url+='&page=1&per_page=50'
        let res= await this.send(url); 
        return res;
    }

    async insetCode(insertData){
        let url='/api/v5/gists';
       
        //post属性
        let postJson={
            access_token:this.access_token,
            files:{},
            /*
            files:({
                'file1.txt':{
                    content:insertData.content,
                }
            }),
            */
            description:insertData.title,
            public:false,
        }
        postJson.files[insertData.title]={
            content:insertData.content,
        }
        postJson=JSON.stringify(postJson);
        console.log(postJson);
        
        let postJson2='{"access_token":"0f807f0cfb04e3ad3c69415c5ac3bbc5","files":{"file1.txt":{"content":"String file contents"}},"description":"0","public":"false"}';
        console.log(postJson2);
        let res= await this.send(url,'POST',postJson); 
        return res;
    }
    /**
     * 更新
     * @param {*} insertData 
     * @returns 
     */
    async updateCode(insertData){
        let url='/api/v5/gists/'+insertData.id;
       
        //post属性
        let postJson={
            access_token:this.access_token,
            files:{},
            /*
            files:({
                'file1.txt':{
                    content:insertData.content,
                }
            }),
            */
            description:insertData.title,
        }
        postJson.files[insertData.title]={
            content:insertData.content,
        }
        postJson=JSON.stringify(postJson);
        console.log(postJson);
        let res= await this.send(url,'PATCH',postJson); 
        return res;
    }
    /**
     * 删除代码片段
     */
     async deleteCode(id){
        //let url='https://gitee.com/api/v5/gists/'+id;
        let url='/api/v5/gists/'+id;
        url+='?access_token='+this.access_token;
        let res= await this.send(url,'DELETE'); 
        return res;
    }
}

module.exports=Gitee;